const rules = Object.create(null)

export function setRule(newRule) {
  Object.assign(rules, newRule)
}

export function getRule(...rest) {
  return rest.reduce((pre, name) => (pre.push(rules[name] || name), pre), [])
}

export function delRule(...rest) {
  for (const name of rest) {
    delete rules[name]
  }
}

setRule({ required: { required: true, message: '必填' } })

export default {
  props: {
    presetRules: {
      type: Object,
      default: () => ({})
    }
  },

  methods: {
    getRule(...rest) {
      return rest.reduce((pre, name) => (pre.push(this.presetRules[name] || rules[name] || name), pre), [])
    }
  }
}
